package com.gzhryc.jdbc;

import java.sql.SQLException;

import com.gzhryc.jdbc.annotaion.BranchTable;
import com.gzhryc.jdbc.models.ConnectInfo;
import com.gzhryc.jdbc.utils.DbUtils;

/**
 * 动态表名，使用{key}做动态替换
 * 
 * @author zhanghao
 *
 * @param <T>
 */
public class DynamicTableDao<T> extends BaseDao<T> {

	Object key;

	public DynamicTableDao(String jdbcKey, Object key) {
		super(jdbcKey);
		this.key = key;
		BranchTable table = clazz.getAnnotation(BranchTable.class);
		this.tableName = table.name().replace("{key}", key.toString());
		ConnectInfo info = ConnectionFactory.getConnectInfo(jdbcKey);
		DbUtils.createTable(info, clazz, tableName);
	}
	
	public int insert(T obj) throws SQLException {
		return jdbcHelper.insert(tableName,obj);
	}

}
